package org.cloud.Controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.cloud.Enum.YesOrNo;
import org.cloud.pojo.*;
import org.cloud.service.CarouselService;
import org.cloud.service.CategoryService;
import org.cloud.service.ItemService;
import org.cloud.utils.IMOOCJSONResult;
import org.cloud.utils.PagedGridResult;
import org.cloud.vo.CategoryVo;
import org.cloud.vo.ItemInfoVo;
import org.cloud.vo.NewItemsVo;
import org.omg.CosNaming.NamingContextPackage.NotEmpty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("items")
@Api(value = "首页",tags = {"首页展示的相关的接口"})
public class ItemController extends BaseController{

    @Autowired
    private ItemService itemService;

    @GetMapping("/info/{itemId}")
    @ApiOperation(value = "查询商品详情",notes = "查询商品详情",httpMethod = "GET")
    public IMOOCJSONResult query(@ApiParam(name = "itemId",value = "商品id",required = true)
                                 @PathVariable String itemId) {
        if (StringUtils.isEmpty(itemId)) {
            return IMOOCJSONResult.errorMsg("暂无商品详情");
        }

        Items items = itemService.queryItemById(itemId);
        List<ItemsImg> itemsImgs = itemService.queryItemImgList(itemId);
        List<ItemsSpec> itemsSpecs = itemService.queryItemSpecList(itemId);
        ItemsParam itemsParam = itemService.queryItemParamList(itemId);

        ItemInfoVo itemInfoVo = new ItemInfoVo();
        itemInfoVo.setItem(items);
        itemInfoVo.setItemParams(itemsParam);
        itemInfoVo.setItemImgList(itemsImgs);
        itemInfoVo.setItemSpecList(itemsSpecs);

        return IMOOCJSONResult.ok(itemInfoVo);
    }

    @GetMapping("/commentLevel")
    @ApiOperation(value = "查询商品评价",notes = "查询商品评价",httpMethod = "GET")
    public IMOOCJSONResult commentLevel(@ApiParam(name = "itemId",value = "商品id",required = true)
                                 @RequestParam String itemId) {
        if (StringUtils.isEmpty(itemId)) {
            return IMOOCJSONResult.errorMsg("暂无商品详情");
        }

        return IMOOCJSONResult.ok(itemService.queryCommentCounts(itemId));
    }

    @GetMapping("/comments")
    @ApiOperation(value = "查询商品评论",notes = "查询商品评论",httpMethod = "GET")
    public IMOOCJSONResult comments(@ApiParam(name = "itemId",value = "商品id",required = true)
                                        @RequestParam String itemId,
            @ApiParam(name = "level",value = "评价等级",required = true)
            @RequestParam Integer level,
            @ApiParam(name = "page",value = "查询下一页的第几页",required = true)
                                    @RequestParam Integer page,
            @ApiParam(name = "pageSize",value = "分页的每一页显示的条数",required = true)
            @RequestParam Integer pageSize) {

        if (StringUtils.isEmpty(itemId)) {
            return IMOOCJSONResult.errorMsg("暂无商品详情");
        }

        if (page == null) {
            page = 1;
        }

        if (pageSize == null) {
            pageSize = COMMENT_PAGE_SIZE;
        }

        PagedGridResult gridResult = itemService.queryPagedComments(itemId,
                level, page, pageSize);

        return IMOOCJSONResult.ok(gridResult);
    }

}
